package com.itheima.edu.map.hashMap;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class HashMapStudentAdmin {
    public static void main(String[] args) {
        HashMap<Student, String> stuMap = new HashMap<>();
        Student s1 = new Student("zhangsan", "23");
        Student s2 = new Student("lisi", "24");
        Student s3 = new Student("wangwu", "25");

        stuMap.put(s1, "南京");
        stuMap.put(s2, "北京");
        stuMap.put(s3, "上海");

        //第一种遍历方式
        //先获取到每一个键
        Set<Student> keys = stuMap.keySet();
        for (Student key : keys) {
            //通过键来获取到它对应的值
            String value = stuMap.get(key);

            System.out.println(key + "---" + value);
        }

        System.out.println("=============================================");

        //第二种遍历方式：
        //使用entry方法获取到每一个键值对对象
        Set<Map.Entry<Student, String>> entries = stuMap.entrySet();
        for (Map.Entry<Student, String> entry : entries) {
            //用键值对的get方法分别获取键和值
            Student key = entry.getKey();
            String value = entry.getValue();

            System.out.println(key + "---" + value);
        }

        System.out.println("=============================================");

        //第三种遍历方式：
        stuMap.forEach(
                (Student s, String address) -> {
                    System.out.println(s + "---" + address);
                }
        );
    }
}
